<?php

//power by wshoto.com
global $_W, $_GPC;

$agentlevels = $this->model->getLevels();
$status = intval($_GPC['status']);
empty($status) && ($status = 1);
$operation = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($operation == 'display') {
	if ($status == -1) {
		ca('commission.apply.view_1');
	} else {
		ca('commission.apply.view' . $status);
	}
	$level = $this->set['level'];
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$condition = ' and a.uniacid=:uniacid and a.status=:status';
	$params = array(':uniacid' => $_W['uniacid'], ':status' => $status);
	if (!empty($_GPC['applyno'])) {
		$_GPC['applyno'] = trim($_GPC['applyno']);
		$condition .= ' and a.applyno like :applyno';
		$params[':applyno'] = "%{$_GPC['applyno']}%";
	}
	if (!empty($_GPC['realname'])) {
		$_GPC['realname'] = trim($_GPC['realname']);
		$condition .= ' and (m.realname like :realname or m.nickname like :realname or m.mobile like :realname)';
		$params[':realname'] = "%{$_GPC['realname']}%";
	}
	if (empty($starttime) || empty($endtime)) {
		$starttime = strtotime('-1 month');
		$endtime = time();
	}
	$timetype = $_GPC['timetype'];
	if (!empty($_GPC['timetype'])) {
		$starttime = strtotime($_GPC['time']['start']);
		$endtime = strtotime($_GPC['time']['end']);
		if (!empty($timetype)) {
			$condition .= " AND a.{$timetype} >= :starttime AND a.{$timetype}  <= :endtime ";
			$params[':starttime'] = $starttime;
			$params[':endtime'] = $endtime;
		}
	}
	if (!empty($_GPC['agentlevel'])) {
		$condition .= ' and m.agentlevel=' . intval($_GPC['agentlevel']);
	}
	if ($status >= 3) {
		$orderby = 'paytime';
	} else {
		if ($status >= 2) {
			$orderby = ' checktime';
		} else {
			$orderby = 'applytime';
		}
	}
	$list = pdo_fetchall("select a.*, m.nickname,m.avatar,m.realname,m.mobile,l.levelname from " . tablename('ewei_shop_commission_apply') . " a " . " left join " . tablename('ewei_shop_member') . " m on m.id = a.mid" . " left join " . tablename('ewei_shop_commission_level') . " l on l.id = m.agentlevel" . " where 1 {$condition} ORDER BY {$orderby} desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
	$total = pdo_fetchcolumn("select count(a.id) from" . tablename('ewei_shop_commission_apply') . " a " . " left join " . tablename('ewei_shop_member') . " m on m.uid = a.mid" . " left join " . tablename('ewei_shop_commission_level') . " l on l.id = m.agentlevel" . " where 1 {$condition}", $params);
	$pager = pagination($total, $pindex, $psize);
}

if ($operation == 'detail') {
	$id = intval($_GPC['id']);
	$apply = $this->model->getCommissionApplyDetail($id);

	if ($apply['status'] == -1) {
		ca('commission.apply.view_1');
	} else {
		ca('commission.apply.view' . $apply['status']);
	}

	if (empty($apply)) {
		message('提现申请不存在!', '', 'error');
	}

	$orderids = iunserializer($apply['orderids']);
	if (!is_array($orderids) || count($orderids) <= 0) {
		message('无任何订单，无法查看!', '', 'error');
	}

	$agentid = $apply['mid'];

	$member = m('member')->getMember($agentid);
	$RecordPrice = $this->model->getCommissionRecordPrice($agentid, array('total', 'ok', 'apply', 'lock', 'check'));

	$agentTeams = $this->model->getCommissionTeamCount($agentid, $this->set['level']);
	$member['agentcount'] = number_format($agentTeams['all'], 0);
	$hasagent = $member['agentcount'] > 0;

	$agentLevel = $this->model->getLevel($apply['mid']);

	if (empty($agentLevel['id'])) {
		$agentLevel = array('levelname' => empty($this->set['levelname']) ? '普通等级' : $this->set['levelname'], 'commission1' => $this->set['commission1'], 'commission2' => $this->set['commission2'], 'commission3' => $this->set['commission3']);
	}

	// 为了适应迁移前的记录。
	$ids = array();
	if(is_array2($orderids)){
		foreach($orderids as $orderid){
			$ids[] = $orderid['orderid'];
		}
	}else{
		$ids = $orderids;
	}

	$lists = $this->model->getCommissionApplyOrderDetail($apply['mid'], $ids);
	$totalcount = count($lists['lists']);
	$list = $lists['lists'];
	$totalorderprice = $lists['totalorderprice'];
	$totalcommission = $lists['totalcommission'];
	$totalpay = $lists['totalpay'];

	if($_W['ispost']){
		$result = array();

		if (checksubmit('submit_check') && $apply['status'] == 1) {
			ca('commission.apply.check');
			$result = $this->model->checkCommissionApply($apply['mid'], $list, $apply);
		}

		if (checksubmit('submit_cancel') && ($apply['status'] == 2 || $apply['status'] == -1)) {
			ca('commission.apply.cancel');
			$result = $this->model->cancelCommissionApply($apply['mid'], $list, $apply);
		}

		if (checksubmit('submit_pay') && $apply['status'] == 2) {
			ca('commission.apply.pay');
			$result = $this->model->payCommissionApply($apply['mid'], $list, $totalpay, $apply);
		}

		if(!empty($result) && $result['status'] == 1){
			message($result['result'], $this->createPluginWebUrl('commission/apply', array('status' => $apply['status'])), 'success');
		}else{
			message($result['result'], $this->createPluginWebUrl('commission/apply', array('status' => $apply['status'])), 'info');
		}
	}

}

load()->func('tpl');
include $this->template('apply');